{layout name="layout"}
{include file="public/nav"}
{css file="/static/admin/css/add/simditor.css" /}
{js file="/static/admin/js/add/module.min.js"}
{js file="/static/admin/js/add/hotkeys.min.js"}
{js file="/static/admin/js/add/uploader.min.js"}
{js file="/static/admin/js/add/simditor.min.js"}
<div class="container-fluid" style="text-align: center">
    <h1>修改文章</h1>
</div>

<div class="container">
    <!--    <form id="pushArticleForm" name="pusharticleform" method="post" action="/admin/pushArticle">-->
    <div class="list-group">
        <label for="changeArticleTitle">主题</label>
        <input class="form-control" placeholder="输入主题" id="changeArticleTitle" type="text" value="{$article.article_name}">
    </div>

    <div class="list-group">
        <label for="changeEditor">编辑文章以发布</label>
        <textarea id="changeEditor" placeholder="输入你想输入的"  autofocus>
            {$article.article_content}
        </textarea>
    </div>

    <div class="list-group">
        <label for="changearticleClass">选择分类</label>
        <span style="color: red"> 可以从下面选择新的分类， 默认为之前的分类 </span><br>
        <select name="changearticleclass" id="changeArticleClass">
            <option value="{$article.class_id}" selected style="color: red">当前所在:{$article.class_name}</option>
            {volist name="$classes", id="class"}
            <option value="{$class.class_id}">{$class.class_name}</option>
            {/volist}
        </select>
    </div>
    <br>
    <div class="list-group">
        <button id="changeArticle" data-changearticle="{$article.article_id}" class="btn btn-success pull-right"> 更新 </button>
    </div>
    <!--    </form>-->
</div>

<br><br><br><br><br>
<br><br><br><br><br>

<script>
    /*select样式*/
    $("#changearticleClass").selectpicker({ width: 200 });
    /*富文本编辑器*/
    var editor = new Simditor({
        textarea: $('#changeEditor')
        //optional options
    });

    // function getSimpleText(html){
    //     var re1 = new RegExp("<.+?>","g");//匹配html标签的正则表达式，"g"是搜索匹配多个符合的内容
    //     return  msg = html.replace(re1,'');//执行替换成空字符
    // }

    // ajax 获取文章内容

    //ajax 修改文章
    $("#changeArticle").click(function () {
        let article_name = document.getElementById("changeArticleTitle").value;
        let article_content = editor.getValue();
        let article_class = parseInt(document.getElementById("changeArticleClass").value);
        var re1 = new RegExp("<.+?>","g");
        if(article_name === '' || $("#changeArticleContent").val() === ''){
            alert("请填写完整所有信息！")
        }else{
            $.ajax({
                type: "POST",
                url: "/admin/changeArticle/"+ $(this).attr("data-changearticle"),
                dataType: "JSON",
                data: {
                    "article_name"          : article_name,
                    "article_content"       : article_content,
                    "article_content_text"  : article_content.replace(re1,''),
                    "article_class"         : article_class,
                },
                success:function (data) {
                    if(data === 1){
                        alert("文章更新成功")
                        // console.log("As")
                    }
                },
                error:function (error) {
                    alert("文章更新失败")
                    console.log(error)
                    // console.log("请求错误")
                }
            })
        }
    })

</script>


